<template>
  <div>
    <a-drawer
      :maskClosable="false"
      :title="title"
      :width="width"
      placement="right"
      :closable="true"
      :bodyStyle="bodyStyle"
      style="top: 0px"
      @close="close"
      :visible="visible">
      <erp-hr-employee-regular-bill-form ref="realForm" @unButtonDisable="unButtonDisable" @ok="submitCallback" @close="closeCallback" :disabled="disableSubmit" normal></erp-hr-employee-regular-bill-form>
      <div class="drawer-footer">
        <a-popconfirm title="确定关闭弹窗吗?" @confirm="handleCancel" :getPopupContainer="(node) => node.parentNode">
          <a-button style="margin-bottom: 0;">关闭</a-button>
        </a-popconfirm>
        <a-button v-if="!disableSubmit" @click="handleOk(true)" type="primary" style="margin-bottom: 0;">提交流程</a-button>
        <a-button v-if="!disableSubmit" @click="handleOk(false)" type="primary" style="margin-bottom: 0;">暂存</a-button>
      </div>
    </a-drawer>
    <a-modal
      :visible="selectFlag"
      title="转正审批单提示"
      @ok="okTemp"
      @cancel="cancelTemp"
      :closable="false"
      cancelText="放弃编辑"
      okText="继续编辑">
      <a-alert
        description="您当前存在未提交的审批单信息，是否继续编辑？点击【放弃编辑】按钮新增审批单信息。"
        type="warning"
        show-icon
      />
    </a-modal>
    <a-modal :visible="flag" title="转正审批单提示" :closable="false" >
      <a-alert
        :description="description"
        type="warning"
        show-icon
      />
      <template slot="footer">
        <a-button @click.stop="()=>{flag=false}">关闭</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>

  import ErpHrEmployeeRegularBillForm from './ErpHrEmployeeRegularBillForm'
  import { getAction } from '@/api/manage'
  import { mapGetters } from 'vuex';

  export default {
    name: 'ErpHrEmployeeRegularBillModal',
    components: {
      ErpHrEmployeeRegularBillForm
    },
    data () {
      return {
        description: '',
        model: {},
        flag: false,
        continueflag: false,
        selectFlag: false,
        title: '操作',
        width: '1000',
        visible: false,
        disableSubmit: false,
        bodyStyle: {
          padding: '20px 20px 70px',
          height: window.innerHeight - 56 + 'px',
          'overflow-y': 'auto'
        },
        url: {
          querySuccessRegularByEmployeeId: '/api/erp/hr/regular/erpHrEmployeeRegularBill/querySuccessRegularByEmployeeId',
          queryTempData: '/api/erp/hr/regular/erpHrEmployeeRegularBill/queryTempData',
          editRegularByEmployeeId: '/api/erp/hr/regular/erpHrEmployeeRegularBill/editRegularByEmployeeId'
        }
      }
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        let userinfo = this.userInfo();
        // 判断是否已经审批成功了
        getAction(this.url.querySuccessRegularByEmployeeId, { employeeId: userinfo.empId }).then((res) => {
          if (res.success) {
            let data = res.result
            if (data.success > 0) {
              this.flag = true;
              this.description = '您的审批单已经审核通过了，请勿重复提交审批！'
            } else if (data.handle > 0) {
              this.flag = true;
              this.description = '您的审批单正在处理中，请勿重复提交审批！'
            } else {
              // 判断是否存在未提交的审批单
              getAction(this.url.queryTempData, { employeeId: userinfo.empId }).then((res) => {
                if (res.success) {
                  let data = res.result.records
                  if (data[0]) {
                    this.model = data[0];
                    this.selectFlag = true;
                  } else {
                    this.title = '员工转正申请单新增'
                    this.visible = true
                    this.selectFlag = false;
                    this.continueflag = false;
                    this.$nextTick(() => {
                      this.$refs.realForm.add();
                    })
                  }
                }
              })
            }
          }
        })
      },
      edit (record) {
        this.title = '员工转正申请单编辑'
        this.visible = true
        this.$nextTick(() => {
          this.$refs.realForm.edit(record);
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      closeCallback () {
        this.close()
      },
      submitCallback() {
        this.$emit('ok');
        this.visible = false;
      },
      handleOk (flag) {
        if (flag) {
          this.$refs.realForm.submitForm();
        } else {
          this.$refs.realForm.submitTempForm();
        }
      },
      okTemp() {
        this.title = '员工转正申请单编辑'
        this.visible = true;
        this.selectFlag = false;
        this.continueflag = true;
        this.$nextTick(() => {
          this.$refs.realForm.edit(this.model);
        })
      },
      cancelTemp() {
        let userinfo = this.userInfo();
        getAction(this.url.editRegularByEmployeeId, { employeeId: userinfo.empId }).then((res) => {
          if (res.success) {
            this.title = '员工转正申请单新增'
            this.visible = true;
            this.selectFlag = false;
            this.continueflag = false;
            this.$nextTick(() => {
              this.$refs.realForm.add();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      unButtonDisable () {
        this.buttonDisable = false
      }
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: 0px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
  /deep/ .ant-modal-footer{
    height: 50px;
  }
</style>
